package com.dataimport.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.TeacherBaseInfo;
import com.dataimport.entity.targetentity.Department;
import com.dataimport.entity.targetentity.Student;
import com.dataimport.entity.targetentity.Teacher;
import com.dataimport.service.TeacherBaseInfoService;
import com.dataimport.mapper.TeacherBaseInfoMapper;
import com.dataimport.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 教师基本信息服务impl
 *
 * @author 邹代鑫
 * @date 2022/09/28
 */
@Service
public class TeacherBaseInfoServiceImpl extends ServiceImpl<TeacherBaseInfoMapper, TeacherBaseInfo> implements TeacherBaseInfoService{

    @Autowired
    private TeacherBaseInfoMapper teacherBaseInfoMapper;

    @Autowired
    private TeacherService teacherService;

    public List<Teacher> selectData(Long currentPage){
        //开启数据库2
        DynamicDataSourceContextHolder.push("mysql2");

        Page<TeacherBaseInfo> page = new Page<>(currentPage,1000);
        Page<TeacherBaseInfo> teacherBaseInfoPage = teacherBaseInfoMapper.selectPage(page, null);
        List<TeacherBaseInfo> teacherBaseInfos = teacherBaseInfoPage.getRecords();
        List<Teacher> teachers = teacherBaseInfos.stream().parallel()
                .filter(new Predicate<TeacherBaseInfo>() {
                    @Override
                    public boolean test(TeacherBaseInfo teacherBaseInfo) {
                        return true;
                    }
                })
                .map(new Function<TeacherBaseInfo, Teacher>() {
                    @Override
                    public Teacher apply(TeacherBaseInfo teacherBaseInfo) {
                        Teacher teacher = new Teacher();
                        teacher.setTeacherCode(teacherBaseInfo.getGh());
                        teacher.setTeacherName(teacherBaseInfo.getXm());
                        teacher.setDepartmentCode(teacherBaseInfo.getSzdwbm());
                        teacher.setEmploymentDate(DateUtil.parse(teacherBaseInfo.getLxrq()));
                        teacher.setStation(teacherBaseInfo.getGw());
                        return teacher;
                    }
                }).collect(Collectors.toList());
        teachers.forEach(System.out::println);
        DynamicDataSourceContextHolder.poll();
        return teachers;
    }

    @Override
    public void dealWithTeacher() {
        long currentPage = 0L;
        while (true) {
            currentPage++;
            List<Teacher> teachers = selectData(currentPage);
            if (ObjectUtils.isEmpty(teachers)) {
                break;
            }
            insert(teachers);
            System.out.println("插入成功");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(List<Teacher> teacherList){
        //开启数据库1
        DynamicDataSourceContextHolder.push("mysql1");
        //插入数据库1
        teacherService.saveBatch(teacherList,teacherList.size());
        DynamicDataSourceContextHolder.poll();
    }
}




